package be.phl.obstacleapp;

import java.util.ArrayList;
import java.util.List;

import be.phl.animation.ActivitySwitcher;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import be.phl.layoutAdapter.PatientRow;
import be.phl.layoutAdapter.PatientRowAdapter;
import be.phl.model.DBAdapterPatient;
import be.phl.model.DBAdapterScreening;

public class PatientsActivity extends ListActivity {

	private static final int PATIENT = 1;
	private static final int NEW_SCREENING = 2;
	private int SCREEN;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);
		// Show the Up button in the action bar.
		getActionBar().setDisplayHomeAsUpEnabled(true);

		SCREEN = getIntent().getIntExtra("Screen", 0);

		List<PatientRow> patients = new ArrayList<PatientRow>();
		DBAdapterPatient db = new DBAdapterPatient(this);
		Cursor c = null;
		db.open();
		switch (SCREEN) {
		case PATIENT:
			this.setTitle("Bewoners");
			c = db.getAllPatients();
			break;
		case NEW_SCREENING:
			this.setTitle("Nieuwe screening");
			c = db.getAllPatientsNoScreening();
			break;
		}
		while (c.moveToNext()) {
			patients.add(new PatientRow(c.getInt(0), c.getString(1) + " "
					+ c.getString(2), c.getString(17) + " " + c.getString(18)
					+ ", " + c.getString(22), c.getInt(7)));
		}
		db.close();

		PatientRowAdapter adapter = new PatientRowAdapter(this, patients);

		setListAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_patients, menu);
		return true;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {

		final PatientRow selected = (PatientRow) l.getAdapter().getItem(
				position);

		switch (SCREEN) {
		case PATIENT:
			// Show patient data
			// start new PatientDetailActivity
			Intent patientAct = new Intent(PatientsActivity.this,
					PatientDetailActivity.class);

			patientAct.putExtra("PatientId", selected.getId());

			PatientsActivity.this.startActivity(patientAct);

			break;

		case NEW_SCREENING:
			// show house

			new AlertDialog.Builder(this)
					.setTitle(
							"Nieuwe screening voor " + selected.getFirstName()
									+ "?")
					.setPositiveButton(R.string.ok,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int id) {
									// start new ScreeningActivity
									Intent screeningAct = new Intent(
											PatientsActivity.this,
											ScreeningActivity.class);

									DBAdapterScreening db = new DBAdapterScreening(
											PatientsActivity.this);
									db.open();
									long screeningId = db.insertScreening(
											selected.getId(),
											selected.getBuildingId());
									db.close();

									screeningAct.putExtra("ScreeningId",
											(int) screeningId);

									PatientsActivity.this
											.startActivity(screeningAct);

								}
							})
					.setNegativeButton(R.string.cancel,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int id) {
									// User cancelled the dialog

								}
							}).show();

			break;
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		// create a new mainactivity
		Intent mainAct = new Intent(this, MainActivity.class);
		this.startActivity(mainAct);

	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	// @Override
	// protected void onResume() {
	// // animateIn this activity
	// ActivitySwitcher.animationIn(findViewById(R.id.totaal),
	// getWindowManager());
	// super.onResume();
	// }

}
